package com.xiaoshuidi.cloud.module.rooms.controller.admin.roomel;

import cn.hutool.core.util.ObjectUtil;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomel.vo.*;
import com.xiaoshuidi.cloud.module.rooms.convert.roomel.RoomelConvert;
import com.xiaoshuidi.cloud.module.rooms.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.rooms.pojo.roomel.RoomelEntity;
import com.xiaoshuidi.cloud.module.rooms.service.IRoomelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.zxp.esclientrhl.repository.PageList;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

/**
 *  房源信息es
 * @author Link
 * @since 2023-05-18
 **/
@Slf4j
@Tag(name = "管理后台 - 房源信息es")
@RequestMapping(ApiConstants.NAME +"/roomel")
@RestController
@Validated
public class RoomelController {
    @Autowired
    private IRoomelService iRoomelService;

    @PostMapping("/listadd")
    @Operation(summary = "房源信息推送es接口")
    @Parameter(name = "roomelBaseVo", description = "123", required = true)
    public CommonResult<Boolean> listadd(@RequestBody List<RoomelCreateReqVO> roomelCreateReqVOList) {
        log.info("接收到的参数为："+roomelCreateReqVOList.toString());
        return success(iRoomelService.listadd(roomelCreateReqVOList));

    }

    @DeleteMapping("/delete")
    @Operation(summary = "房源信息es接口-根据es的id删除房源")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteByesid(@RequestParam("id") String id) {
        log.info("传入的id："+id);
        Boolean result = iRoomelService.deleteByesid(id);
        return success(result);
    }

    @PutMapping("/update")
    @Operation(summary = "房源信息es接口-更新")
    //覆盖更新（nested的原因），后台自动根据ID查询相关信息
    public CommonResult<Boolean> updateCover(@Valid @RequestBody RoomelUpdateReqVO roomelUpdateReqVO) {
        Boolean result = iRoomelService.updateCover(roomelUpdateReqVO);
        return success(result);

    }

    @PutMapping("/updateCoverBatch")
    @Operation(summary = "房源信息es接口-批量更新")
    //覆盖更新（nested的原因），后台自动根据ID查询相关信息
    public CommonResult<Boolean> updateCoverBatch(@Valid @RequestBody List<RoomelUpdateReqVO> updateReqVOList) {
        Boolean result = iRoomelService.updateCoverBatch(updateReqVOList);
        return success(result);

    }

    @GetMapping("/getRoomelList")
    @Operation(summary = "房源信息es查询")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:roomhouse:query')")
    public CommonResult<List<RoomelEntity>> getRoomelList(@RequestParam("ids") Long [] ids) {
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        List<RoomelEntity> list = iRoomelService.getRoomelList(ids, tenantId);
        return CommonResult.success(list);
    }

    /**
     *  房源信息es接口-分页查询
     **/
    @Operation(summary = "房源信息es接口-分页查询")
    @GetMapping("/queryPage")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:roomhouse:query')")
    public CommonResult<PageResult<RoomelQueryResp>> queryPage(@Valid RoomelQueryReqVO reqVO){
        log.info("进入，房源信息es接口-分页查询："+reqVO.toString());
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        return iRoomelService.queryPage(reqVO, tenantId);
    }

    /**
     *  房态计数查询
     **/
    @Operation(summary = "房态计数查询")
    @GetMapping("/queryHouseStateSum")
    @Parameters({
            @Parameter(name = "apartmentAreaId",description = "小区id"),
            @Parameter(name = "apartmentId",description = "公寓id"),
            @Parameter(name = "buildingNo",description = "楼栋号"),
            @Parameter(name = "floorNo",description = "楼层号"),
            @Parameter(name = "roomHouseTypeId",description = "户型"),
            @Parameter(name = "layoutId",description = "房型"),
            @Parameter(name = "houseState",description = "房态"),
            @Parameter(name = "searchbox",description = "搜索框"),
            @Parameter(name = "roomUsage",description = "房源类型"),
            @Parameter(name = "qflag",description = "查询类型（1：集中式。2：分散式）",required = true),
    })
    @PreAuthorize("@ss.hasAnyPermissions('rooms:roomhouse:query')")
    public CommonResult<Map<String,Object>> queryHouseStateSum( Long roomAreaId, Long apartmentAreaId, String apartmentAreaName,Long apartmentId ,String buildingNo ,Integer floorNo ,Long roomHouseTypeId,
                                                                Long layoutId,String houseState,String searchbox , String roomUsage, Boolean qflag){
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        Map<String,Object> result = iRoomelService.queryHouseStateSum(roomAreaId, apartmentAreaId, apartmentAreaName,apartmentId ,
                buildingNo ,floorNo ,roomHouseTypeId, layoutId,houseState,searchbox,roomUsage, qflag, tenantId);

        return success(result);
    }

    /**
     *  动态选项卡-楼栋并计数查询
     **/
    @Operation(summary = "动态选项卡-楼栋")
    @GetMapping("/queryBuildingNoSum")
    @Parameters({
            @Parameter(name = "apartmentAreaId",description = "小区id"),
            @Parameter(name = "apartmentId",description = "公寓id"),
            @Parameter(name = "buildingNo",description = "楼栋号"),
            @Parameter(name = "floorNo",description = "楼层号"),
            @Parameter(name = "roomHouseTypeId",description = "户型"),
            @Parameter(name = "layoutId",description = "房型"),
            @Parameter(name = "houseState",description = "房态"),
            @Parameter(name = "searchbox",description = "搜索框"),
            @Parameter(name = "qflag",description = "查询类型（1：集中式。2：分散式）"),
    })
    @PreAuthorize("@ss.hasAnyPermissions('rooms:roomhouse:query')")
    public CommonResult<List<QueryBuildingNoRespVO>> queryBuildingNoSum(Long roomAreaId, Long apartmentAreaId, String apartmentAreaName,Long apartmentId ,String buildingNo ,Integer floorNo ,Long roomHouseTypeId,
                                                                Long layoutId,String houseState,String searchbox , Boolean qflag, Integer pageNum, Integer pageSize){
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        List<QueryBuildingNoRespVO> result = iRoomelService.queryBuildingNoSum(roomAreaId, apartmentAreaId, apartmentAreaName,apartmentId ,
                buildingNo ,floorNo ,roomHouseTypeId, layoutId,houseState,searchbox, qflag, tenantId, pageNum, pageSize);

        return success(result);
    }

    /**
     *  动态选项卡-单元
     **/
    @Operation(summary = "动态选项卡-单元")
    @GetMapping("/query-unit-no")
    @Parameters({
            @Parameter(name = "apartmentAreaId",description = "小区id"),
            @Parameter(name = "apartmentId",description = "公寓id"),
            @Parameter(name = "buildingNo",description = "楼栋号"),
            @Parameter(name = "unitNo",description = "单元号"),
            @Parameter(name = "floorNo",description = "楼层号"),
            @Parameter(name = "roomHouseTypeId",description = "户型"),
            @Parameter(name = "layoutId",description = "房型"),
            @Parameter(name = "houseState",description = "房态"),
            @Parameter(name = "searchbox",description = "搜索框"),
            @Parameter(name = "qflag",description = "查询类型（1：集中式。2：分散式）"),
    })
    @PreAuthorize("@ss.hasAnyPermissions('rooms:roomhouse:query')")
    public CommonResult<List<QueryUnitNoRespVO>> queryUnitNoSum( Long roomAreaId, Long apartmentAreaId, String apartmentAreaName,Long apartmentId ,String buildingNo , String unitNo, Integer floorNo ,Long roomHouseTypeId,
                                                                         Long layoutId,String houseState,String searchbox , Boolean qflag, Integer pageNum, Integer pageSize){
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        List<QueryUnitNoRespVO> result = iRoomelService.queryUnitNoSum(roomAreaId, apartmentAreaId, apartmentAreaName,apartmentId ,
                buildingNo ,unitNo, floorNo ,roomHouseTypeId, layoutId,houseState,searchbox, qflag, tenantId, pageNum, pageSize);
        return success(result);
    }

    /**
     *  动态选项卡-楼层查询
     **/
    @Operation(summary = "动态选项卡-楼层查询")
    @GetMapping("/queryFloorTab")
    @Parameters({
            @Parameter(name = "apartmentAreaId",description = "小区id"),
            @Parameter(name = "apartmentId",description = "门店id"),
            @Parameter(name = "buildingNo",description = "楼栋号"),
            @Parameter(name = "unitNo",description = "单元号"),
            @Parameter(name = "floorNo",description = "楼层号"),
            @Parameter(name = "roomHouseTypeId",description = "户型"),
            @Parameter(name = "layoutId",description = "房型"),
            @Parameter(name = "houseState",description = "房态"),
            @Parameter(name = "searchbox",description = "搜索框"),
            @Parameter(name = "qflag",description = "查询类型（1：集中式。2：分散式）"),
    })
    @PreAuthorize("@ss.hasAnyPermissions('rooms:roomhouse:query')")
    public CommonResult<List<QueryFloorTabRespVO>> queryFloorTab( Long roomAreaId, Long apartmentAreaId, String apartmentAreaName,Long apartmentId ,String buildingNo, String unitNo,Integer floorNo ,Long roomHouseTypeId,
                                                           Long layoutId,String houseState,String searchbox , Boolean qflag, Integer pageNum, Integer pageSize){
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        List<QueryFloorTabRespVO> result = iRoomelService.queryFloorTab(roomAreaId, apartmentAreaId, apartmentAreaName,apartmentId ,
                buildingNo, unitNo, floorNo,roomHouseTypeId, layoutId,houseState,searchbox,qflag, tenantId, pageNum,pageSize);

        return success(result);
    }
    /**
     *  动态选项卡-房型查询
     **/
    @Operation(summary = "动态选项卡-房型查询")
    @GetMapping("/queryFxTab")
    @Parameters({
            @Parameter(name = "apartmentAreaId",description = "小区id"),
            @Parameter(name = "apartmentId",description = "公寓id"),
            @Parameter(name = "buildingNo",description = "楼栋号"),
            @Parameter(name = "floorNo",description = "楼层号"),
            @Parameter(name = "roomHouseTypeId",description = "户型"),
            @Parameter(name = "layoutId",description = "房型"),
            @Parameter(name = "houseState",description = "房态"),
            @Parameter(name = "searchbox",description = "搜索框"),
            @Parameter(name = "qflag",description = "查询类型（1：集中式。2：分散式）"),
    })
    @PreAuthorize("@ss.hasAnyPermissions('rooms:roomhouse:query')")
    public CommonResult<List<QueryFxTabRespVO>> queryFxTab( Long roomAreaId, Long apartmentAreaId, String apartmentAreaName,Long apartmentId ,String buildingNo ,Integer floorNo ,Long roomHouseTypeId,
                                          Long layoutId,String houseState,String searchbox , Boolean qflag, Integer pageNum, Integer pageSize){
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        List<QueryFxTabRespVO> result = iRoomelService.queryFxTab(roomAreaId, apartmentAreaId, apartmentAreaName,apartmentId ,
                buildingNo ,floorNo ,roomHouseTypeId, layoutId,houseState,searchbox, qflag, tenantId, pageNum, pageSize);

        return success(result);
    }
    /**
     *  动态选项卡-户型查询
     **/
    @Operation(summary = "动态选项卡-户型查询")
    @GetMapping("/queryHxTab")
    @Parameters({
            @Parameter(name = "apartmentAreaId",description = "小区id"),
            @Parameter(name = "apartmentId",description = "公寓id"),
            @Parameter(name = "buildingNo",description = "楼栋号"),
            @Parameter(name = "floorNo",description = "楼层号"),
            @Parameter(name = "roomHouseTypeId",description = "户型"),
            @Parameter(name = "layoutId",description = "房型"),
            @Parameter(name = "houseState",description = "房态"),
            @Parameter(name = "searchbox",description = "搜索框"),
            @Parameter(name = "qflag",description = "查询类型（1：集中式。2：分散式）"),
    })
    @PreAuthorize("@ss.hasAnyPermissions('rooms:roomhouse:query')")
    public CommonResult<List<QueryHxTabRespVO>> queryHxTab( Long roomAreaId, Long apartmentAreaId, String apartmentAreaName,Long apartmentId ,String buildingNo ,Integer floorNo ,Long roomHouseTypeId,
                                          Long layoutId,String houseState,String searchbox, Boolean qflag, Integer pageNum, Integer pageSize){
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        List result = iRoomelService.queryHxTab(roomAreaId, apartmentAreaId, apartmentAreaName,apartmentId ,
                buildingNo ,floorNo ,roomHouseTypeId, layoutId,houseState,searchbox, qflag, tenantId, pageNum, pageSize);

        return success(result);
    }
    /**
     *  动态选项卡-小区查询
     **/
    @Operation(summary = "动态选项卡-小区查询")
    @GetMapping("/queryXqTab")
    @Parameters({
            @Parameter(name = "apartmentAreaId",description = "小区id"),
            @Parameter(name = "apartmentId",description = "公寓id"),
            @Parameter(name = "buildingNo",description = "楼栋号"),
            @Parameter(name = "floorNo",description = "楼层号"),
            @Parameter(name = "roomHouseTypeId",description = "户型"),
            @Parameter(name = "layoutId",description = "房型"),
            @Parameter(name = "houseState",description = "房态"),
            @Parameter(name = "searchbox",description = "搜索框"),
            @Parameter(name = "qflag",description = "查询类型（1：集中式。2：分散式）"),
    })
    @PreAuthorize("@ss.hasAnyPermissions('rooms:roomhouse:query')")
    public CommonResult<List<QueryXqTabRespVO>> queryXqTab(Long roomAreaId, Long apartmentAreaId, String apartmentAreaName, Long apartmentId , String buildingNo , Integer floorNo , Long roomHouseTypeId,
                                                           Long layoutId, String houseState, String searchbox , Boolean qflag, Integer pageNum, Integer pageSize){
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        List<QueryXqTabRespVO> result = iRoomelService.queryXqTab(roomAreaId, apartmentAreaId, apartmentAreaName,apartmentId ,
                buildingNo ,floorNo ,roomHouseTypeId, layoutId,houseState,searchbox, qflag, tenantId, pageNum, pageSize);

        return success(result);
    }

    /**
     *  房源信息es-详情查询
     **/
    @Operation(summary = "房源信息es-详情查询")
    @GetMapping("/queryById")
    @Parameters({
            @Parameter(name = "id",description = "房间id",required = true),
    })
    @PreAuthorize("@ss.hasAnyPermissions('rooms:roomhouse:query')")
    public CommonResult<RoomelQueryResp> queryById(Long id){
        log.info("进入，房源信息es-详情查询,房间id： "+id);
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        RoomelEntity search = iRoomelService.queryById(id, tenantId);
        RoomelQueryResp ret = RoomelConvert.INSTANCE.convert(search);
        return success(ret);
    }
}
